<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page="page" :permission="permissionList"
               :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave"
               @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange" @size-change="sizeChange" @on-load="onLoad">
      <template slot-scope="{type,size,row,index}" slot="menuBtn">
        <el-dropdown-item divided @click.native="isStatuClick(row)">是否禁用</el-dropdown-item>
      </template>
      <template slot="menuLeft">
        <UploadStyle
          ref="uploadStyle"
          v-if="screenShotDialogVisible"
          :visible="screenShotDialogVisible"
          :img-url="imgUrl"
          :proportion="proportion"
          @closeDialog="closeDialog"
          @getImgURL="getImgURL"
        />
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove, school, lockorUnlockPlate} from "@/api/forum/plate";
import {mapGetters} from "vuex";
import iconList from "@/config/iconList";

export default {
  data() {
    return {
      screenShotDialogVisible: false,
      imgUrl: "",
      _done: null,
      proportion: [686, 386],// 宽高的像素数组
      _uploadLoading: null,
      form: {},
      query: {},
      dialogVisible: false,
      isThrottle: false,
      typeRdeio: 1,
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 7,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        labelWidth: 120,
        menuType: 'menu',
        menuBtnTitle: '更多操作',
        column: [
          // {
          //   label: "主键",
          //   prop: "id",
          //   rules: [{
          //     required: true,
          //     message: "请输入主键",
          //     trigger: "blur"
          //   }],
          //   hide: true,
          //   display: false
          // },
          // {
          //   label: "学校名称",
          //   prop: "schoolId",
          //   search: true,
          //   cascader: ['visitPlatePermissionGroup','issuePermissionGroup','notRestrictedPermissionsGroup'],
          //   rules: [{
          //     required: true,
          //     message: "请输入学校名称",
          //     trigger: "blur"
          //   }],
          //   type: "tree",
          //   dicUrl: "/api/PcPublicService/GetSchoolTreeSelect",
          //   props: {
          //     label: "xxName",
          //     value: "id"
          //   },
          // },
          {
            label: "板块名称",
            prop: "name",
            search: true,
            maxlength: 20,
            showWordLimit: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入板块名称",
              trigger: "blur"
            }],
          },
          {
            label: "板块id",
            prop: "id",
            // search: true,
            // maxlength: 20,
            editDisplay:false,
            viewDisplay:false,
            addDisplay:false,
            showWordLimit: true,
            overHidden: true,
            rules: [{
              required: false,
              message: "请输入板块名称",
              trigger: "blur"
            }],
          },
          {
            label: "板块简介",
            prop: "desc",
            maxlength: 20,
            showWordLimit: true,
            overHidden: true,
            hide:true,
            rules: [{
              required: true,
              message: "请输入板块简介",
              trigger: "blur"
            }],
          },
          {
            label: "板块图标",
            prop: "logo",
            type: "icon",
            slot: true,
            hide:true,
            iconList: iconList,
            rules: [
              {
                required: true,
                message: "请输入板块图标",
                trigger: "click"
              }
            ]
          },
          {
            label: "板块排序",
            prop: "sort",
            type: "number",
            width: 80,
            rules: [
              {
                required: true,
                message: "请输入板块排序",
                trigger: "blur"
              }
            ],
            tip: '数字越大越大越靠前'
          },

          {
            label: "是否需要审核",
            prop: "isAudit",
            type: 'select',
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择是否需要审核",
              trigger: "blur"
            }],
            dicData: [{
              label: '是',
              value: true
            }, {
              label: '否',
              value: false
            }],
            value: true

          },
          // {
          //   label: "访问权限",
          //   prop: "visitPlatePermission",
          //   type: 'select',
          //   overHidden: true,
          //   rules: [{
          //     required: true,
          //     message: "请选择访问权限",
          //     trigger: "blur"
          //   }],
          //   dicData: [{
          //     label: '公开',
          //     value: 0
          //   }, {
          //     label: '指定用户组访问',
          //     value: 1
          //   }],
          //   value: 0,
          //   change: ({value}) => {
          //     const visitPlatePermissionGroup = this.findObject(this.option.column, "visitPlatePermissionGroup");
          //     school.hide = true;
          //     if(value === 0){
          //       visitPlatePermissionGroup.editDisplay = false
          //       visitPlatePermissionGroup.viewDisplay = false
          //       visitPlatePermissionGroup.addDisplay = false
          //       visitPlatePermissionGroup.rules[0].required = false
          //       this.form.visitPlatePermissionGroup = []
          //     }else if(value === 1){
          //       visitPlatePermissionGroup.editDisplay = true
          //       visitPlatePermissionGroup.viewDisplay = true
          //       visitPlatePermissionGroup.addDisplay = true
          //       visitPlatePermissionGroup.rules[0].required = true
          //     }
          //     this.form.visitPlatePermission = value
          //   }
          // },
          // {
          //   label: "用户组",
          //   prop: "visitPlatePermissionGroup",
          //   editDisplay:false,
          //   viewDisplay:false,
          //   addDisplay:false,
          //   hide:true,
          //   rules: [{
          //     required: false,
          //     message: "请选择用户组",
          //     trigger: "blur"
          //   }],
          //   multiple: true,
          //   checkStrictly: true,
          //   type: "tree",
          //   dicUrl: "/api/ManagePlate/GetUserGroupSelect?schoolId={{key}}",
          //   props: {
          //     label: "groupName",
          //     value: "id"
          //   },
          // },
          {
            label: "发帖权限",
            prop: "issuePermission",
            type: 'select',
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择发帖权限",
              trigger: "blur"
            }],
            dicData: [{
              label: '公开',
              value: 0
            }, {
              label: '指定用户组发帖',
              value: 1
            }],
            value: 0,
            change: ({value}) => {
              const issuePermissionGroup = this.findObject(this.option.column, "issuePermissionGroup");
              school.hide = true;
              if(value === 0){
                issuePermissionGroup.editDisplay = false
                issuePermissionGroup.viewDisplay = false
                issuePermissionGroup.addDisplay = false
                issuePermissionGroup.rules[0].required = false
                this.form.issuePermissionGroup = []
              }else if(value === 1){
                issuePermissionGroup.editDisplay = true
                issuePermissionGroup.viewDisplay = true
                issuePermissionGroup.addDisplay = true
                issuePermissionGroup.rules[0].required = true
              }
              this.form.issuePermission = value
            }
          },
          {
            label: "用户组",
            prop: "issuePermissionGroup",
            editDisplay:false,
            viewDisplay:false,
            addDisplay:false,
            multiple: true,
            hide:true,
            checkStrictly: true,
            rules: [{
              required: false,
              message: "请选择用户组",
              trigger: "blur"
            }],
            type: "tree",
            dicUrl: "/api/ManagePlate/GetUserGroupSelect",
            props: {
              label: "groupName",
              value: "id"
            },
          },
          {
            label: "帖子访问权限",
            prop: "visitArticlePermission",
            type: 'select',
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择帖子访问权限",
              trigger: "blur"
            }],
            dicData: [{
              label: '所有帖子可见',
              value: 0
            }, {
              label: '指定级别访问',
              value: 1
            }],
            value: 0,
            change: ({value}) => {
              const visitArticlePermissionLevel = this.findObject(this.option.column, "visitArticlePermissionLevel");
              school.hide = true;
              if(value === 0){
                visitArticlePermissionLevel.editDisplay = false
                visitArticlePermissionLevel.viewDisplay = false
                visitArticlePermissionLevel.addDisplay = false
                visitArticlePermissionLevel.rules[0].required = false
                this.form.visitArticlePermissionLevel = 0
              } else if (value === 1) {
                visitArticlePermissionLevel.editDisplay = true
                visitArticlePermissionLevel.viewDisplay = true
                visitArticlePermissionLevel.addDisplay = true
                visitArticlePermissionLevel.rules[0].required = true
                visitArticlePermissionLevel.value = 1
                this.form.visitArticlePermissionLevel = 1
              }
              this.form.visitArticlePermission = value
            }
          },
          {
            label: "级别",
            prop: "visitArticlePermissionLevel",
            type: 'select',
            hide:true,
            overHidden: true,
            editDisplay:false,
            viewDisplay:false,
            addDisplay:false,
            rules: [{
              required: false,
              message: "请选择级别",
              trigger: "blur"
            }],
            value: 1,
            dicData: [{
              label: '可见本班数据',
              value: 3
            }, {
              label: '可见全年级数据',
              value: 2
            }, {
              label: '可见全校数据',
              value: 1
            }],
          },
          {
            label: "不受权限控制的用户组",
            prop: "notRestrictedPermissionsGroup",
            multiple: true,
            hide:true,
            checkStrictly: true,
            rules: [{
              required: false,
              message: "请选择用户组",
              trigger: "blur"
            }],
            type: "tree",
            dicUrl: "/api/ManagePlate/GetUserGroupSelect",
            props: {
              label: "groupName",
              value: "id"
            },
          },
          {
            label: "状态",
            prop: "status",
            type: 'select',
            overHidden: true,
            rules: [{
              required: false,
              message: "请选择状态",
              trigger: "blur"
            }],
            dicData: [{
              label: '禁用',
              value: 0
            }, {
              label: '正常',
              value: 1
            }],
            value: 1
          },
          {
            label: "板块banner图",
            prop: "banner",
            rules: [{
              required: true,
              message: "请选择板块banner图",
              trigger: "blur"
            }],
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            listType: 'picture-img',
            span: 24,
            accept: "image/*",
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true,
            uploadBefore: (file, done, loading, col) => {
              this.screenShotDialogVisible = true
              const fileReader = new FileReader()
              fileReader.onload = (res) => {
                // 给图片裁切传递参数
                this.imgUrl = res.target.result
              }
              fileReader.readAsDataURL(file)
              // 备份
              this._done = done
              this._uploadLoading = loading
            }
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", "JSuserInfo"]),
    permissionList() {
      return {
        addBtn: true,//this.vaildData(this.permission.bj_add, false),
        viewBtn: true,//this.vaildData(this.permission.bj_view, false),
        delBtn: true,//this.vaildData(this.permission.bj_delete, false),
        editBtn: true
      };
    },
    schoolId() {
      return this.JSuserInfo.schoolId
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    // this.getSchool()
  },
  methods: {
    //是否禁用
    isStatuClick(row){
      let message = row.status === 0?'是否确定启用此板块':'是否确定禁用此板块'
      this.$confirm(message, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return lockorUnlockPlate({id: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    rowSave(row, done, loading) {
      row.id = '0'
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: this.ids});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      console.log(type)
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      // this.query.schoolId = this.schoolId
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    closeDialog(val){
      if(this._uploadLoading !== null){
        this._uploadLoading();
        this._uploadLoading = null
      }
      this.screenShotDialogVisible = val;
    },
    getImgURL(data){
      if(this._done !== null){
        this._done(data);
        this._done = null
      }
      this.closeDialog(false)
    }

  }
};
</script>

<style></style>
